/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IR;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Rectangle;
import java.awt.ScrollPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.TableModel;



/**
 *
 * @author ev
 */
public class Gui extends JFrame{

    public JButton button;
    public JTable table;
    public JTextField field;
    public JComboBox options;
    public JButton open;
    public JButton next;
    public JButton prev;
    public JScrollPane scrollpane;
    
    
    private String[] desOpt = {"Name", "Owner", "Contents"};
    public String[] pages = {"Go to page"};
    
    public Gui() {
        initUI();
    }
    
    public final void initUI() {

        JPanel panel = new JPanel();

        
        button = new JButton("Search");
        button.setBounds(670, 30, 100, 30);
        button.addActionListener(new ActionListener() {
                        @Override
			public void actionPerformed(ActionEvent e) {
 
				// Execute when button is pressed
                                String[] head = {"score", "filename", "owner", "path"};
                                try {
                                    DefaultTableModel model = new DefaultTableModel(SearchFiles.Search(field.getText(), options.getSelectedItem().toString().toLowerCase()), head);
                                    table.setModel(model);
                                } catch (Exception ex) {
                                    Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                
                                prev.setEnabled(true);
                                next.setEnabled(true);
                                
			}
		});
        
        
        prev = new JButton("Previous");
        prev.setBounds(10, 350, 100, 30);
        prev.setEnabled(false);
        prev.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            Rectangle rect = scrollpane.getVisibleRect();
                            int blockIncr = scrollpane.getViewport().getViewRect().height;
                            JScrollBar  bar = scrollpane.getVerticalScrollBar();
                            
                            bar.setValue(bar.getValue() - blockIncr);
                            scrollpane.scrollRectToVisible(rect);
                        }
                });
        
        next = new JButton("Next");
        next.setBounds(670, 350, 100, 30);
        next.setEnabled(false);
        next.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            Rectangle rect = scrollpane.getVisibleRect();
                            int blockIncr = scrollpane.getViewport().getViewRect().height;
                            JScrollBar  bar = scrollpane.getVerticalScrollBar();
                            
                            bar.setValue(bar.getValue() + blockIncr);
                            scrollpane.scrollRectToVisible(rect);
                        }
                });
        
        field = new JTextField();
        field.setBounds(10, 30, 350, 30);
        
        options = new JComboBox();
        options.setBackground(Color.white);
        options.setBounds(370, 30, 280, 30);
        for (int i = 0; i < desOpt.length; i++)
            options.addItem(desOpt[i]);
        
        open = new JButton("Open");
        //page.setBackground(Color.white);
        open.setBounds(350, 350, 100, 30);
        //page.addItem(pages[0]);
        open.setEnabled(false);
        open.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                        try {
                            Runtime runtime = Runtime.getRuntime();
                            Process process = runtime.exec("E:/notepad++/notepad++.exe " + table.getValueAt(table.getSelectedRow(), 2).toString());
                        } catch (IOException ex) {
                            Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        }
                });
        
        
        JLabel label = new JLabel("Enter query in the field below :");
        label.setBounds(10, 5, 700, 20);
        
        String[] colHeadings = {"score", "filename", "owner", "path"};
        int numRows = 0 ;
        DefaultTableModel model = new DefaultTableModel(numRows, colHeadings.length) ;
        model.setColumnIdentifiers(colHeadings);
        
        table = new JTable(model);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 1) {
                open.setEnabled(true);
            }
            };
        });

        
        
        
        
//        table.setBounds(10, 90, 760, 250);
        
        scrollpane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollpane.setBounds(10, 90, 760, 245);
        
       
        
        panel.add(button);
        panel.add(field);
        panel.add(label);
        panel.add(scrollpane);
        panel.add(options);
        panel.add(prev);
        panel.add(open);
        panel.add(next);
        
        getContentPane().add(panel);
        panel.setLayout(null);
        
        setTitle("Information Retrieval");
        setSize(800, 430);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Gui ex = new Gui();
                ex.setVisible(true);
            }
        });
    }
}



